//
//  NSObject+BaseFramework.m
//  LotteryClient
//
//  Created by Sunyon 14-3-31.
//  Copyright (c) 2014年 CTTC. All rights reserved.
//

#import "NSObject+BaseFramework.h"
#import <objc/runtime.h>

@implementation NSObject (BaseFramework)


+ (void)swizzleSelector:(SEL)originalSelector withSelector:(SEL)swizzledSelector
{

    Class class = [self class];

    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

    BOOL didAddMethodInit = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));

    if (didAddMethodInit) 
	{
        class_addMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    }
	else
	{
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}


+ (void)swizzleClassSelector:(SEL)originalSelector withSelector:(SEL)swizzledSelector
{

	Class class = [self class];
	
	Method originalMethod = class_getClassMethod(class, originalSelector);
	Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
	
	BOOL didAddMethodInit = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
	
	if (didAddMethodInit) 
	{
		class_addMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
	}
	else
	{
		method_exchangeImplementations(originalMethod, swizzledMethod);
	}
}


@end

#pragma mark -
#pragma mark -PropertyList

@implementation NSObject (PropertyList)

- (NSDictionary *)getPropertyNameAndValue
{
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (i = 0; i < outCount; i++)
	{
        objc_property_t property = properties[i];
		NSString *propertyName = [NSString stringWithCString:property_getName(property) 
													encoding:NSUTF8StringEncoding];        
		id propertyValue = [self valueForKey:(NSString *)propertyName];
        if (propertyValue) [props setObject:propertyValue forKey:propertyName];
    }
    free(properties);
    return props;
}

+ (NSDictionary *)getPropertyNameAndTypeDic
{
    NSMutableArray* pronames = [NSMutableArray array];
    NSMutableDictionary* resultDic = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (i = 0; i < outCount; i++) 
	{
        objc_property_t property = properties[i];
        NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        [pronames addObject:propertyName];
        NSString *propertyType = [NSString stringWithCString: property_getAttributes(property) encoding:NSUTF8StringEncoding];
        /*
         c char
         i int
         l long
         s short
         d double
         f float
         @ id //指针 对象
         ...  BOOL 获取到的表示 方式是 char
         .... ^i 表示  int*  一般都不会用到
         */
        if ([propertyType hasPrefix:@"T@"])
		{

			NSUInteger local = [propertyType rangeOfString:@","].location-4;
			if (local > 0)
			{
				propertyType = [propertyType substringWithRange:NSMakeRange(3, local)];
			}
			else
			{
				propertyType = @"NSObject";
			}

        }
        if ([propertyType hasPrefix:@"Ti"])
		{
            propertyType = @"int";
        }
        if ([propertyType hasPrefix:@"Tf"])
		{
            propertyType = @"float";
        }
		if ([propertyType hasPrefix:@"Tc"])
		{
            propertyType = @"BOOL";
        }
		
		if ([propertyType length] == 0)
		{
			propertyType = @"NSObject";
		}

		[resultDic setObject:propertyType forKey:propertyName];
		//        DLog(@"%@,%@",propertyName,propertyType);

    }
    free(properties);

    return resultDic;
}


@end